var dateTemplate = new Template("\#{month}-\#{day}-\#{year} \#{hour}:\#{minute}:00");
var rowTemplate = new Template("<tr class='\#{class}'><td /><td>\#{name}</td></tr>");
var inviteTemplate = new Template("<tr class='\#{class}'><td /><td>\#{name}</td><td><div class='tip'><a href='#' onclick=\"revokeInvite('\#{action}', this)\">uninvite</a></div></td></tr>");
var fileTemplate = new Template("<a href='\#{href}'>\#{filename}</a>");
var autoTemplate = new Template("<a href='#' class='tip' onclick='this.next(\"div\").show(); this.hide();'>invite more</a><div style='display: none'><input type='text' class='autocomplete' name='query' style='width:120px' /><div class='autocomplete results' style='z-index: 100' /></div>");

function revokeInvite(action, a){
    var that = a;
    new Element('form', {
        action: action
    }).request({
        onComplete: function(){
            that.up('tr').hide();
        }
    });
}

function toggleFrequency(prefix){
    if ($(prefix ? prefix + 'repeatBox' : 'repeatBox').checked) 
        $$('tr.repeatOptions').invoke('show');
    else 
        $$('tr.repeatOptions').invoke('hide');
    if ($('ajaxForm')) 
        resizeLightviewAjax();
}

function addCalendarEvent(event, day, action, ajaxForm, calId, adding, start){
    if (event) 
        Event.stop(event);
    
    var params;
    if (ajaxForm) 
        params = Form.serialize(ajaxForm, true);
    else if ($('ajaxForm')) 
        params = Form.serialize('ajaxForm', true);
    else 
        params = $H({
            "cal.id": calId,
            "ajax": true
        });
    
    if (day != null) 
        dayOffset = day;
    else 
        dayOffset = null;
    
    if (adding || event) 
        lightbox(action, params, function(transport){
            if ($('ajaxForm')) {
                $$('input.datepicker').each(function(elem){
                
                    if (dayOffset != null) {
                        start.setDate(start.getDate() + dayOffset);
                        
                        var picker = new Control.DatePicker(elem, {
                            timePicker: true,
                            timePickerAdjacent: true,
                            date: startTime,
                            parent: elem
                        });
                    }
                    else {
                        var picker = new Control.DatePicker(elem, {
                            timePicker: true,
                            timePickerAdjacent: true,
                            parent: elem
                        });
                    }
                    
                    if (dayOffset != null) {
                        elem.value = dateTemplate.evaluate({
                            month: ((start.getMonth() + 1 < 10) ? '0' : '') +
                            (start.getMonth() + 1),
                            day: ((start.getDate() < 10) ? '0' : '') +
                            start.getDate(),
                            year: start.getYear() + 1900,
                            hour: ((start.getHours() < 10) ? '0' : '') +
                            start.getHours(),
                            minute: ((start.getMinutes() < 10) ? '0' : '') +
                            start.getMinutes()
                        });
                        picker.options.date = Control.DatePicker.DateFormat.parseFormat(elem.value, picker.options.currentFormat);
                        start.setDate(start.getDate() - dayOffset);
                    }
                });
                
                dayOffset = null;
                $('ajaxForm').observe('submit', function(e){
                    addCalendarEvent(e, null, action, null, calId, true, start);
                });
                Event.observe(document, 'protoplasm:opened', function(){
                    resizeLightviewAjax(0, 250);
	            });
                Event.observe(document, 'protoplasm:closed', function(){
                    resizeLightviewAjax(0, 0);
                });
				Event.observe(document, 'lightview:hidden', function(){
					Event.stopObserving(document, 'protoplasm:opened');
					Event.stopObserving(document, 'protoplasm:closed');
					Event.stopObserving(document, 'lightview:hidden');
				});
            }
            else 
                eval(transport);
        });
}
